package com.mls.fivedistrict.controller.backend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.NewsFlash;
import com.mls.fivedistrict.server.NewsFlashService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/6/5 15:33
 */
@RestController("backendNewsFlashController")
@RequestMapping("/api/manager")
public class NewsFlashController extends BaseController {
    @Autowired
    NewsFlashService newsFlashService;

    /**
     * 快讯添加
     * @param newsFlash
     * @return
     */
    @PostMapping("/v1/newsFlash")
    public ServiceResponse addNewsFlash(@RequestBody NewsFlash newsFlash){
        BeanValidator.check(newsFlash);
        boolean insert = newsFlashService.insert(newsFlash);
        if(insert){
            return  ServiceResponse.createSuccessResponse("添加成功!");
        }else{
            return ServiceResponse.createErrorResponse("添加失败!");
        }
    }

    /**
     * 快讯删除
     * @param newsFlashId
     * @return
     */
    @DeleteMapping("/v1/newsFlash/{newsFlashId}")
    public ServiceResponse deleteNewsFlash(@PathVariable Long newsFlashId){
        Optional.ofNullable(newsFlashId).orElseThrow(() -> new CustomerException("要删除的快讯id不能为空!"));
        NewsFlash newsFlash = new NewsFlash();
        newsFlash.setId(newsFlashId);
        newsFlash.setDelete(true);
        boolean delete = newsFlashService.updateById(newsFlash);
        if(delete){
            return  ServiceResponse.createSuccessResponse("删除成功!");
        }else{
            return ServiceResponse.createErrorResponse("删除失败!");
        }
    }
    @PutMapping("/v1/newsFlash/{newsFlashId}")
    public ServiceResponse updateNewsFlash(@PathVariable Long newsFlashId,@RequestBody NewsFlash newsFlash){
        Optional.ofNullable(newsFlashId).orElseThrow(() -> new CustomerException("要修改的快讯id不能为空!"));
        Optional.ofNullable(newsFlash).orElseThrow(() -> new CustomerException("要修改的快讯不能为空!"));
        newsFlash.setId(newsFlashId);
        boolean update = newsFlashService.updateById(newsFlash);
        if(update){
            return  ServiceResponse.createSuccessResponse("修改成功!");
        }else{
            return ServiceResponse.createErrorResponse("修改失败!");
        }
    }

    /**
     * 分页查询列表
     * @return
     */
    @GetMapping("/v1/newsFlashes")
    public ServiceResponse getNewsFlashList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = newsFlashService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<NewsFlash>(selectList, 5);
        return  ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 单个快讯详情
     * @param newsFlashId
     * @return
     */
    @GetMapping("/v1/newsFlash/{newsFlashId}")
    public ServiceResponse getNewsFlashDetail(@PathVariable Long newsFlashId){
        Optional.ofNullable(newsFlashId).orElseThrow(() -> new CustomerException("要查询的快讯id不能为空!"));
        NewsFlash newsFlash = newsFlashService.selectById(newsFlashId);
        if(null!=newsFlash){
            return ServiceResponse.createSuccessResponse(newsFlash);
        }else{
            return ServiceResponse.createErrorResponse("查无此快讯!");
        }
    }
}
